package com.zq.zqplayer.test

import android.os.Bundle
import android.view.SurfaceHolder
import android.view.View
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.zq.playerlib.service.PlayerItemInfo
import com.zq.playerlib.service.StatusListener
import com.zq.playerlib.service.ZQPlayerServiceWrap
import com.zq.zqplayer.databinding.ActivityZqPlayerServiceTestBinding
import com.zq.zqplayer.util.VideoDataUtil

class ZQPlayerServiceTestActivity : BaseActivity() {

    private val binding : ActivityZqPlayerServiceTestBinding by binding()
    private var zqPlayerServiceWrap: ZQPlayerServiceWrap? = null
    var playeriteminfo = PlayerItemInfo()

    var mSurfaceHolder: SurfaceHolder? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.root
        initData(savedInstanceState)
    }

    fun initData(savedInstanceState: Bundle?) {
        zqPlayerServiceWrap = ZQPlayerServiceWrap(this)
        playeriteminfo.url = VideoDataUtil.getTestVideoPath(this.application)
        binding.surfaceView.holder.addCallback(object : SurfaceHolder.Callback {

            override fun surfaceCreated(surfaceHolder: SurfaceHolder) {
                mSurfaceHolder = surfaceHolder
                binding.btnInit.isEnabled = true
                binding.btnPlayWindow.isEnabled = true
            }

            override fun surfaceChanged(surfaceHolder: SurfaceHolder, i: Int, i1: Int, i2: Int) {
                mSurfaceHolder = surfaceHolder
                binding.btnInit.isEnabled = true
                binding.btnPlayWindow.isEnabled = true
            }

            override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) {
                mSurfaceHolder = null
            }
        })

        binding.btnInit.click {
            binding.surfaceView.visibility = View.GONE  // surface 会 重新 建立
            binding.surfaceView.visibility = View.VISIBLE
            zqPlayerServiceWrap?.init(playeriteminfo, mSurfaceHolder?.surface!!, object : StatusListener.Stub(){
                override fun onLoading() {
                }

                override fun onPause() {
                }

                override fun onStop() {
                    runOnUiThread {
                        binding.btnPlay.isEnabled = false
                        binding.btnStop.isEnabled = true
                    }
                }

                override fun onError(msg: String?) {
                }

                override fun onPrepareFinished() {
                    runOnUiThread {
                        binding.btnPlay.isEnabled = true
                    }
                }

                override fun onPlaying() {
                    runOnUiThread {
                        binding.btnStop.isEnabled = true
                    }
                }

            })
        }

        binding.btnPlay.click {
            zqPlayerServiceWrap?.play()
        }
        binding.btnStop.click {
            zqPlayerServiceWrap?.stop()
        }
        binding.btnPlayWindow.click {
            zqPlayerServiceWrap?.showFloatingWindow()
        }
    }


    override fun onDestroy() {
        zqPlayerServiceWrap?.destroy()
        super.onDestroy()
    }





}